package com.zwps.common.api.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 自定义参数选项校验器
 * 
 * @ClassName: OptionValidator
 * @Description:
 * @author lyh
 * @date 2023年5月23日 下午3:17:14
 *
 */
public class IntOptionValidator implements ConstraintValidator<IntOption, Integer> {
    private int[] options;

    @Override
    public void initialize(IntOption option) {
        options = option.value();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        if (options == null || options.length == 0) {
            return false;
        }
        for (int option : options) {
            if (option == value) {
                return true;
            }
        }
        return false;
    }

}
